package bb.lanxing.view;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;

import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;

import bb.lanxing.R;

/**
 * 在图片上方写字并画出矩形框
 */
public class InfoWindowToBitmap {
    public static BitmapDescriptor getBitmapDescriptor(String str, Resources res) {
        Bitmap bluePoint = BitmapFactory.decodeResource(res, R.drawable.bg_team_info_selected);
        int width = bluePoint.getWidth();
        int height = bluePoint.getHeight();
        int color = bluePoint.getPixel(width / 2, height / 2);

        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444); //建立一个空的Bitmap

        Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);//抗锯齿
        textPaint.setDither(true); // 获取跟清晰的图像采样
        textPaint.setFilterBitmap(true);// 过滤
        textPaint.setColor(Color.WHITE);
        textPaint.setTextSize(40);

        Rect textRect = new Rect();
        textPaint.getTextBounds(str, 0, str.length(), textRect);
        int infoWindowRectHeight = textRect.height() + 10;
        bitmap = Bitmap.createScaledBitmap(bitmap, textRect.width() + 20,
                infoWindowRectHeight + bitmap.getHeight() + 5, true);

        Paint paintRect = new Paint(Paint.ANTI_ALIAS_FLAG);
        paintRect.setStyle(Paint.Style.FILL_AND_STROKE);
        paintRect.setColor(Color.rgb(Color.red(color), Color.green(color), Color.blue(color)));
        RectF infoWindowRect = new RectF(0, 0, bitmap.getWidth(), textRect.height() + 10);
        Canvas canvas = new Canvas(bitmap);
        canvas.drawRoundRect(infoWindowRect, 10, 10, paintRect);        //绘制圆角矩形
        canvas.drawText(str, 10, textRect.height(), textPaint);
        canvas.drawBitmap(bluePoint, (bitmap.getWidth() - width) / 2, infoWindowRect.height() + 5, null);
        return BitmapDescriptorFactory.fromBitmap(bitmap);
    }
}